using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Edge.Common.Utils;
using Edge.Common.Utils;

namespace Edge.Common.Web.DataSourceAdapters
{
    public abstract class BaseDataSource : IDataSource
    {
        public const string DEFAULT = "Default";
        private const string SORT_EXPRESSION_KEY = "OldSortExpression";
        private BaseDataSourceView view;
        private GridView gridView;
        private string SORT_DIRECTION_KEY = "OldSortDirection";

        private bool initialized = false;

        public BaseDataSource(GridView gridView)
        {
            if (gridView == null)
                throw new NotImplementedException();

            gridView.DataSource = this;

            this.gridView = gridView;

            Init();

        }

        protected BaseDataSource(GridView gridView, bool init)
        {
            if (gridView == null)
                throw new NotImplementedException();

            gridView.DataSource = this;

            this.gridView = gridView;

            if (init)
                Init();
        }

        public event EventHandler DataSourceChanged;

        protected void Init()
        {
            if (initialized)
                return;




            gridView.Load += delegate(object sender, EventArgs e)
                                 {
                                     LoadSortState();
                                 };

            if (GetViewInternal().CanPage)
                gridView.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e)
                                                  {
                                                      gridView.PageIndex = e.NewPageIndex;
                                                      gridView.DataBind();
                                                  };
            if (GetViewInternal().CanSort)
                gridView.Sorting += delegate(object sender, GridViewSortEventArgs e)
                                        {
                                            if (!gridView.AllowSorting)
                                                return;

                                            bool swapDirection = false;
                                            swapDirection = GetViewInternal().SortExpression == e.SortExpression;

                                            GetViewInternal().SortExpression = e.SortExpression;

                                            if (swapDirection)
                                            {
                                                GetViewInternal().SortDirection =
                                                    EnumUtils.Swap(SortDirection.Ascending, SortDirection.Descending,
                                                                   GetViewInternal().SortDirection);
                                            }
                                            else
                                            {
                                                GetViewInternal().SortDirection =
                                                    e.SortDirection == System.Web.UI.WebControls.SortDirection.Ascending
                                                        ?
                                                    SortDirection.Ascending
                                                        :
                                                    SortDirection.Descending;
                                            }

                                            gridView.DataBind();

                                            SaveSortState();
                                        };

            GetViewInternal().SelectExecuted += delegate(object sender, BaseDataSourceView.SelectedEventArgs e)
                                                    {
                                                        SaveSortState();
                                                    };

            initialized = true;

        }

        public DataSourceView GetView(string viewName)
        {
            return GetViewInternal();
        }

        protected BaseDataSourceView GetViewInternal()
        {
            if (view == null)
                view = CreateDataSourceView();
            return view;
        }

        protected abstract BaseDataSourceView CreateDataSourceView();

        protected void LoadSortState()
        {
            StateBag viewState =
                (StateBag)
                ReflectionUtils.GetNonPublicPropertyValue(gridView, "ViewState");

            SortDirection direction = ReflectionUtils.Cast(viewState[SORT_DIRECTION_KEY], SortDirection.Ascending);
            GetViewInternal().SortDirection = direction;
            string expression = ReflectionUtils.Cast(viewState[SORT_EXPRESSION_KEY], string.Empty);
            GetViewInternal().SortExpression = expression;
        }

        protected void SaveSortState()
        {
            StateBag viewState =
                (StateBag)
                ReflectionUtils.GetNonPublicPropertyValue(gridView, "ViewState");

            viewState[SORT_DIRECTION_KEY] = GetViewInternal().SortDirection;
            viewState[SORT_EXPRESSION_KEY] = GetViewInternal().SortExpression;
        }

        public ICollection GetViewNames()
        {
            throw new NotImplementedException();
        }
    }
}
